package com.rgzn;

import java.math.BigInteger;
import java.time.Instant;

public class Player {
    String name;
    private BigInteger score;
    int attack;
    volatile boolean isPlaying;
    private long startTime;
    private BigInteger lastScore;

    public Player(String name) {
        this.name = name;
        this.score = BigInteger.ZERO;
        this.isPlaying = false;
        this.attack = 10;
        this.lastScore = BigInteger.ZERO;
    }

    public void playGame() {
        System.out.println(this.name + "开始游戏");
        this.isPlaying = true;
        this.startTime = Instant.now().getEpochSecond();
        Thread scoreDisplayThread = new Thread(() -> {
            while(true) {
                if (this.isPlaying) {
                    try {
                        this.showScore();
                        Thread.sleep(1000L);
                        continue;
                    } catch (InterruptedException var2) {
                    }
                }

                return;
            }
        });
        scoreDisplayThread.start();
    }

    public void stopGame() {
        this.isPlaying = false;
        System.out.println(this.name + "游戏结束");
    }

    public void attack() {
        this.score = this.score.add(BigInteger.valueOf((long)this.attack));
    }

    public void showScore() {
        long elapsedSeconds = Instant.now().getEpochSecond() - this.startTime;
        if (elapsedSeconds > 0L) {
            BigInteger currentScore = this.score.subtract(this.lastScore);
            this.lastScore = this.score;
            String var10001 = this.name;
            System.out.println(var10001 + "每秒平均得分：" + String.valueOf(currentScore.divide(BigInteger.valueOf(elapsedSeconds))));
        } else {
            System.out.println(this.name + "每秒平均得分：0");
        }

    }
}
